package com.example.poemtest.mine;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.poemtest.R;

import java.util.List;


/**
 * 我的问题列表适配器
 * created by JHT on 2023/5/12.
 */
public class QuestionListViewAdapter extends BaseAdapter {
    private Context mContext;
    private int layoutResId;
    private List<Question> questions;

    public QuestionListViewAdapter(Context mContext, int layoutResId, List<Question> questions) {
        this.mContext = mContext;
        this.layoutResId = layoutResId;
        this.questions = questions;
    }

    @Override
    public int getCount() {
        if (questions != null){
            return questions.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (questions != null){
            return questions.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            //加载item的布局文件，生成一个View对象，再给converView赋值
            convertView = LayoutInflater.from(mContext).inflate(layoutResId,null);
            //获取每一个item中的控件对象
            viewHolder = new ViewHolder();
            viewHolder.question = convertView.findViewById(R.id.question);
            viewHolder.answer = convertView.findViewById(R.id.answer);
            viewHolder.time = convertView.findViewById(R.id.time);
            viewHolder.imageView = convertView.findViewById(R.id.img);
            //将ViewHolder对象同converView关联起来
            convertView.setTag(viewHolder);
        }else {//重复利用convertView对象
            //获取到跟当前的convertView绑定在一起的ViewHolder对象
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //给控件对象绑定要显示的数据
        viewHolder.question.setText(questions.get(position).getDesc());
        viewHolder.answer.setText(questions.get(position).getAnswer());
        viewHolder.time.setText(questions.get(position).getDate());
        viewHolder.imageView.setImageBitmap(questions.get(position).getImg());
        return convertView;
    }
    private static class ViewHolder{
        //每一个item中的控件
        private TextView question, answer, time;
        private ImageView imageView;
    }
}
